<?php

declare (strict_types = 1);

namespace app\common\service;

use app\common\service\BaseService;
use app\common\exception\AdminException;
use app\common\repository\VersionRepository;

class VersionService extends BaseService
{
   public function __construct(VersionRepository $repository)
   {
       $this->repository = $repository;
   }

    /**
     * 获取应用升级列表
     *
     * @param array $params
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     */
    public function getList(array $params = []): array
    {
        [$page, $pageSize] = $this->getPaginationParams();
        $where = [];
        if(!empty($params['version'])){
            $where[] = ['version', 'like', '%'.$params['version'].'%'];
        }
        return $this->repository->paginate($where, ['id' => 'desc'], $page, $pageSize);
    }

    /**
     * 获取应用版本详情信息
     *
     * @param $id
     * @return \app\common\model\BaseModel|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getInfo($id): ?\app\common\model\BaseModel
    {
        return $this->repository->getOne($id);
    }

    /**
     * 创建应用版本信息
     *
     * @param array $data
     * @return \app\common\model\BaseModel|array
     * @throws \Exception
     */
    public function create(array $data): \app\common\model\BaseModel|array
    {
        return $this->repository->save($data);
    }

    /**
     * 更新应用版本信息
     * 
     * @param array $data
     * @param $id
     * @return bool|int
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function update(array $data, $id): bool|int
    {
        if(empty($id)) {
            throw new AdminException('ID 不能为空');
        }
        return $this->repository->update($data, $id);
    }

    /**
     * 删除应用版本信息
     *
     * @param $id
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function delete($id): bool
    {
        return $this->repository->delete($id);
    }

    /**
     * 检查应用版本升级
     *
     * @param $version
     * @return array|\app\common\model\BaseModel|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
   public function checkVersion($version): array|\app\common\model\BaseModel|null
   {
       $versionInfo = $this->repository->getOne(['status' => 1], ['*'], ['create_time' => 'desc']);
       if(!$versionInfo) {
           throw new AdminException('检查升级异常，请稍后重试！');
       }
       if($versionInfo['version'] !== $version) {
           $versionInfo['msg'] = 'version:' . $versionInfo['version'];
            return $versionInfo;
       }

       return [
           'msg' => '已经是最新版本',
           'status' => 0
       ];
   }
}